package cn.com.dstz.admin.action;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

import cn.com.dstz.utils.ActionUtils;
import cn.com.dstz.utils.DataBaseUtils;
import cn.com.dstz.utils.NumberUtils;
import cn.com.dstz.utils.ValidUtils;
import cn.com.dstz.web.constants.Constants;
import cn.com.dstz.base.action.BaseAction;

/**
 * 用户分组Action
 * @author 叶装装
 *
 */
public class MemberGroupAct extends BaseAction{

	public void list( ){
		
		Map< String, Object > valueMap = new HashMap< String, Object >( );
		valueMap.put( "ismanager", 0 );
		
		String sql = "select * from dstzgroup g, dstz_group_type t where g.id = t.groupid and t.ismanager=:ismanager";
		
		this.responseToJsonObject( this.paginationBySQL( sql, null, valueMap,  null )  );
		
		return;
	}
	
	public void add( ){
		
		int validSaveOrUpdate = validSaveOrUpdate( 0 );
		
		if( validSaveOrUpdate > 0 ){
			this.ajaxResponse( validSaveOrUpdate );
			return;
		}
		
		int response = 0;
		
		int save = baseService.save( "dstzgroup", DataBaseUtils.getData( new String[]{
				"name"
		}, new Object[]{
				name
		} ), true );
		
		if( save > 0 ){
			// 此处并不需要判断是否为管理员， 因为除了管理员之外，其他用户组并不能对allowread进行操作， allowread 默认是1，即为允许的。 
			// 只有allowread 不为空的时候， 系统才返回该字段的值
			int saveGroup = baseService.save( "dstz_group_type", DataBaseUtils.getData( new String[]{
					"groupid", "allowlogin", "ismanager", "level", "allowread", "isdefault"
			}, new Object[]{
					save, 0, 0, 0, getAllowread( ), getIsdefault( )
			} ) );
			// 如果这个保存失败，则删除原来的
			if( saveGroup <= 0 ){
				delete( save );
			}
			
			response = saveGroup;
		}
		
		this.addLog( response, "membergroup", 0, name );
		
		this.ajaxResponse( response > 0 ? 0 : -1 );
		
		return;
		
	}
	
	public String toadd( ){
		return "add";
	}
	
	public void edit( ){
		// 用户组的编辑并不需要更新type表
		int validSaveOrUpdate = validSaveOrUpdate( 1 );
		if( validSaveOrUpdate > 0 ){
			this.ajaxResponse( validSaveOrUpdate );   // 返回错误消息
			return;
		}
		
		int response = 0;
		
		response = baseService.update( "dstzgroup", DataBaseUtils.getData( new String[]{
				"name"
		}, new Object[]{
				name
		} ), DataBaseUtils.getData( new String[]{
				"id"
		}, new Object[]{
				id
		} ) );
		
		
		Map< String, Object > admin = this.getEntityInSession( Constants.ADMIN_IN_SESSION );
		int level = ActionUtils.getAdminLevel( admin );
		
		if( level == 0 && response > 0 ){
			response = baseService.update( "dstz_group_type", DataBaseUtils.getData( new String[]{
					"allowread", "isdefault"
			}, new Object[]{
					getAllowread( ), getIsdefault( )
					
			} ), DataBaseUtils.getData( new String[]{
					"groupid"
			}, new Object[]{
					id
			} ) );
		}
		
		this.addLog( response, "membergroup", 1, "ID：" + id );
		
		this.ajaxResponse( response > 0 ? 0 : -1 ); 
		return;
		
	}
	
	public String toedit( ){
		// 转到编辑页面也不需要读取type表的信息
		int id = NumberUtils.getInt( getParameter("id"), 0 );
		if( id > 0 ){
			String sql = "select * from dstzgroup g, dstz_group_type t where g.id = t.groupid and id=:groupid";
			Map< String, Object > data = baseService.queryForMap( sql, DataBaseUtils.getData( new String[]{"groupid"}, new Object[]{id} ) );
			getRequest( ).setAttribute( "data", data );
		}
		
		return "edit";
	}
	
	/**
	 * 删除就要删除全部的
	 */
	public void delete( ){
		
		int validSaveOrUpdate = validSaveOrUpdate( 2 );
		
		if( validSaveOrUpdate > 0 ){
			this.ajaxResponse( validSaveOrUpdate );
			return;
		}
		
		Map< String, Object > admin = this.getEntityInSession( Constants.ADMIN_IN_SESSION );
		int level = ActionUtils.getAdminLevel( admin ); // 查看等级
		// 对系统管理员不进行此判断
		if( level > 0 ){
			String sql = "select * from dstzgroup g, dstz_group_type t where g.id = t.groupid and t.groupid=:groupid";
			Map< String, Object > data = baseService.queryForMap( sql, DataBaseUtils.getData( new String[]{"groupid"}, new Object[]{id} ) );
			
			if( data == null || data.isEmpty( ) ){
				this.ajaxResponse( 3 );
				return;
			}
			
			Byte isdefault = ( Byte ) data.get( "isdefault" );
			if( isdefault == 1  ){
				this.ajaxResponse( 4 );
				return;
			}
		}
		
		
		
		int count = baseService.countBySQL( "select count(*) as a from dstzmember where groupid=:groupid", DataBaseUtils.getData( new String[]{
				"groupid"
		}, new Object[]{
				id
		} ) );
		
		if( count > 0 ){
			this.ajaxResponse( 1 );
			return; // 被引用
		}
		
		int del = baseService.deleteBySql( "dstzgroup", DataBaseUtils.getData( new String[]{
				"id"
		}, new Object[]{
				id
		} ) );
		
		if( del > 0 ){
			del = baseService.deleteBySql( "dstz_group_type", DataBaseUtils.getData( new String[]{
				"groupid"
			}, new Object[]{
					id
			} ) );
		}
		
		this.addLog( del, "membergroup", 1, "ID：" + id );
		
		this.ajaxResponse( del > 0 ? 0 : -1 );
		
	}

	private int delete( int id ){
		
		return baseService.deleteBySql( "dstzgroup", DataBaseUtils.getData( new String[]{
				"id"
		}, new Object[]{
				id
		} ) );
		
	}
	
	private int validSaveOrUpdate( int saveOrUpdate ){
		// 验证数据是否存在， 编辑或者删除的
		if( saveOrUpdate == 2 ){
			if( id == null || id == 0 ){
				return 3;
			}
			return 0;
		}
		
		if( ValidUtils.validLength( name, 1, 32 )){
			return 1;
		}
		
		int count = 0;
		
		if( saveOrUpdate == 0 ){
			count = dbValid( );
		}else if( saveOrUpdate == 1 ){
			if( id == null || id == 0 ){
				return 3;
			}
			if( !StringUtils.equals( name, oldname )){
				count = dbValid( );
			}
		}
		
		if( count > 0 ){
			return 2;
		}
		
		return count;
		
	}
	
	private int dbValid( ){
		String sql = "select count(*) as a from dstzgroup where name=:name";
		
		return baseService.countBySQL( sql, DataBaseUtils.getData( new String[]{
				"name"
		}, new Object[]{
				name
		} ) );
	}
	
	private static final long serialVersionUID = 1L;
	
	private Integer id;
	
	private String name;
	private String oldname;
	private Integer allowread;
	
	private Integer isdefault;
	
	// 默认情况下为不是系统默认
	public Integer getIsdefault( ) {
		if( isdefault == null ){
			isdefault = 0;
		}
		return isdefault;
	}

	public void setIsdefault( Integer isdefault ) {
		this.isdefault = isdefault;
	}

	/**
	 * @return
	 */
	public Integer getAllowread( ) {
		if( allowread == null ){
			allowread = 1; //  数据库中默认的就是为允许读取
		}
		return allowread;
	}

	public void setAllowread( Integer allowread ) {
		this.allowread = allowread;
	}

	public String getName( ) {
		return name;
	}

	public void setName( String name ) {
		if( StringUtils.isNotBlank( name ) ){
			name = decode( name ); // 进行解码操作
		}
		this.name = name;
	}

	public String getOldname( ) {
		return oldname;
	}

	public void setOldname( String oldname ) {
		if( StringUtils.isNotBlank( oldname ) ){
			oldname = decode( oldname ); // 进行解码操作
		}
		this.oldname = oldname;
	}

	public Integer getId( ) {
		return id;
	}

	public void setId( Integer id ) {
		if( id == null ){
			id = 0;
		}
		this.id = id;
	}
}
